package com.demo.language_vs;

import org.junit.jupiter.api.Test;

/**
 * <a href="https://www.yuque.com/shenguangyang/coep2g/uiemde">文档</a>
 * @author shenguangyang
 */
public class VsGo {
    static class E {
        int a;

        E(int a) {
            this.a = a;
        }
    }

    /**
     * vs 频繁内存分配/垃圾回收
     */
    @Test
    public void test1() {
        long t1 = System.currentTimeMillis();
        final long ARRAY_COUNT = 10000L;
        final long TEST_COUNT = ARRAY_COUNT * 100000L;

        E[] es = new E[(int)ARRAY_COUNT];
        for(long i = 0; i < TEST_COUNT; i++)
            es[(int)(i * 123456789L % ARRAY_COUNT)] = new E((int)i);

        long n = 0;
        for(long i = 0; i < ARRAY_COUNT; i++) {
            E e = es[(int)i];
            if(e != null)
                n += e.a;
        }
        System.out.println(n);
        long t2 = System.currentTimeMillis();
        System.out.println("java run time: " + (t2 - t1) + " ms");
    }
}
